import csv
import re

def extract_last_numeric(data):
    # 使用正则表达式提取最后一个非空纯数字串
    pattern = r'(\d+)(?!.*\d)'  # 正向否定预查，匹配最后一个非空纯数字串
    matches = re.findall(pattern, data)
    if matches:
        return matches[-1]
    else:
        return ''

def process_csv(source_file, target_file):
    with open(source_file, 'r') as source_csv, open(target_file, 'w', newline='') as target_csv:
        reader = csv.reader(source_csv)
        writer = csv.writer(target_csv)

        for row in reader:
            if len(row) >= 2:  # 检查是否有足够的列数据
                data = row[1]  # 第二列数据

                last_numeric = extract_last_numeric(data)  # 提取最后一个非空纯数字串

                updated_row = row + [last_numeric]  # 创建更新后的行，将最后一个非空纯数字串追加到原始行数据后面
                writer.writerow(updated_row)  # 写入目标文件

    print("数据复制成功！")

# 设置源文件路径和目标文件路径
source_file = "Emo/meta/EmoMV1.csv"
target_file = "Emo/meta/EmoMV3.csv"

# 调用函数进行数据复制
process_csv(source_file, target_file)
